#ifndef T1_CRYPTO_H_
#define T1_CRYPTO_H_

#include "iodef.h"
#include "regs.h"

typedef struct {
        __IO uint8_t WBUF[256];
        __IO uint8_t RBUF[256];
        __IO uint32_t CCFG;
        __IO uint32_t Reserved;
        __IO uint32_t INT_EN;
        __IO uint32_t INT_MSK;
        __IO uint32_t INT_STA;
        __IO uint32_t INT_MSKED;
        __IO uint32_t INT_CLR;
}crypto_reg_t;


/* ---------------------Macro Definition of Crypto Register Files ---------------*/
/* CCFG */
#define RSA_LDEXP                   _BIT(0)
#define RSA_LDBASE                  _BIT(1)
#define RSA_START                   _BIT(2)
#define AES_ENC_START               _BIT(3)
#define AES_DEC_LDK                 _BIT(4)
#define AES_DEC_START               _BIT(5)
#define DES_START                   _BIT(6)
#define SHA256_WR0                  _BIT(7)
#define SHA256_WR1                  _BIT(8)
#define SHA256_RD                   _BIT(9)
#define RSA_EN                      _BIT(16)
#define AES_ENC_EN                  _BIT(17)
#define AES_DEC_EN                  _BIT(18)
#define DES_EN                      _BIT(19)
#define DES_DEC_EN                  _BIT(20)
#define SHA256_EN                   _BIT(21)

/* INT_EN/INT_MSK/INT_STA/INT_MSKED/INT_CLR */
#define RSA_LDEXP_DONE              _BIT(0)
#define RSA_LDBASE_DONE        	    _BIT(1)
#define RSA_DONE                    _BIT(2)
#define AES_ENC_DONE                _BIT(3)
#define AES_DEC_LDK_DONE      	    _BIT(4)
#define AES_DEC_DONE                _BIT(5)
#define DES_DONE                    _BIT(6)
#define SHA256_DONE                 _BIT(7)


/* --------------------- Macro Definition of DES Encryption  ---------------*/
#define DES    			    ((crypto_reg_t*)T1_ENCRYPT_BASE)

/*WBUF*/
#define DES_IN_ST           0 	/*DES KEY: [63:0] of DES->WBUF*/
#define DES_IN_END			7
#define DES_IN_LEN			8 	/*bytes*/

#define DES_KEY_ST          8 	/*DES KEY: [127:64] of DES->WBUF*/
#define DES_KEY_END         15
#define DES_KEY_LEN         8 	/*bytes*/

#define DES_IN              ((uint8_t *)&(DES->WBUF[DES_IN_ST]))
#define DES_KEY             ((uint8_t *)&(DES->WBUF[DES_KEY_ST]))

/*RBUF*/


#define DES_OUT_ST          0 	/*DES KEY: [63:0] of DES->WBUF*/
#define DES_OUT_END         7
#define DES_OUT_LEN         8 	/*bytes*/

#define DES_OUT             ((uint8_t *)&(DES->RBUF[DES_OUT_ST]))


/* --------------------- Macro Definition of AES128 Encryption  ---------------*/
#define AES    			    ((crypto_reg_t*)T1_ENCRYPT_BASE)

#define AES_MODE_ENC        0
#define AES_MODE_DEC        1

/*AES Input -> WBUF  [127:0]*/
#define AES_INS             0   	/*AES Input Start Byte*/
#define AES_INE             15  	/*AES Input End Byte*/
#define AES_INLEN           16   	/*AES Input Len*/

/*AES Input -> WBUF  [255:128]*/
#define AES_KEYS		    16 	/*AES Key Start Byte*/
#define AES_KEYE		    31 	/*AES Key End Byte*/
#define AES_KEYLEN          16 	/*AES Key Len*/

#define AES_IN              ((uint8_t *)&(AES->WBUF[AES_INS]))
#define AES_KEY			    ((uint8_t *)&(AES->WBUF[AES_KEYS]))

/*AES Output -> RBUF  [127:0]*/
#define AES_OUTS		    0   	/*AES Output Start Byte*/
#define AES_OUTE	        15  	/*AES Output End Byte*/
#define AES_OUTLEN	        16  	/*AES Output Len*/

#define AES_OUT			    ((uint8_t *)&(AES->RBUF[AES_OUTS]))


/* --------------------- Macro Definition of RSA Encryption  ---------------*/
#define RSA                 ((crypto_reg_t*)T1_ENCRYPT_BASE)

#define RSA_REG_LEN         256     /*RSA Reg length(bytes)*/

/*WBUF [2048:0] */
#define RSA_INS		        0   	/*RSA Input Start Byte*/
#define RSA_INE	            255 	/*RSA Input End Byte*/
#define RSA_INLEN	        256 	/*Input Lens*/

#define RSA_IN              ((uint8_t *)&(RSA->WBUF[RSA_INS]))

/*RBUF [2048:0]*/
#define RSA_OUTS	        0   	/*RSA Onput Start Byte*/
#define RSA_OUTE            255 	/*RSA Onput End Byte*/
#define RSA_OUTLEN          256 	/*Onput Lens*/

#define RSA_OUT             ((uint8_t *)&(RSA->RBUF[RSA_OUTS]))


/* --------------------- Macro Definition of Sha256 Encryption  ---------------*/
#define SHA256              ((crypto_reg_t*)T1_ENCRYPT_BASE)

/*WBUF [511:0] */
#define SHA256_INS          0   	/*SHA256 Input Start Byte*/
#define SHA256_INE          63  	/*SHA256 Input End Byte*/
#define SHA256_INLEN        64  	/*Input Lens*/


#define SHA256_IN           ((uint8_t *)&(SHA256->WBUF[SHA256_INS]))

/*RBUF [255:0]*/
#define SHA256_OUTS         0   	/*SHA256 Output Start Byte*/
#define SHA256_OUTE         31  	/*SHA256 Output End Byte*/
#define SHA256_OUTLEN       32  	/*Output Lens*/

#define SHA256_OUT          ((uint8_t *)&(SHA256->RBUF[SHA256_OUTS]))

#endif /* T1_CRYPTO_H_ */
